<?php
if (!defined('BASEPATH')) exit ('No direct script access allowed');
/**
 * Progressive Play Server
 * Player Stats Model
 * /models/player_model.php
 * By: Yukwunhang
 */
class Player_model extends Model
{
    // Constructor
    function Player_model()
    {
        parent::Model();
    }

    /**
     * Get top players' stats
     * @return array
     * @param object $number[optional]
     * @param object $offset[optional]
     * @param object $sort_key[optional]
     */
    function get_top_player_stats($number = 40, $offset = 0, $sort_key = 'rating')
    {
        $this->db->select('user_id, name, kills, deaths, kd, tier, cg, rating, old_rating');
        $this->db->from('players');
		$this->db->where('mu != ', 25); // Only show rated players
		$this->db->where('sigma != ', 8.3);
        $this->db->order_by($sort_key, 'desc');
        $this->db->limit($number, $offset);

        // Run query
        $query = $this->db->get();
        $result = $query->result_array();

        return $result;
    }
	
	/**
	 * Get player stats by ID.
	 * @return an array of player stats
	 * @param object $pid
	 */
	function get_player_stats_by_id($user_id)
	{
		$query = $this->db->get_where('players', array('user_id' => $user_id));
		$result = $query->row_array();
		
		if (!$result)
		{
			return FALSE;
		}
		// Fix USSOCOM <-> Selfkill naming bug
		$this->load->helper('array');
		$result = array_change_key_name('Selfkill', 'USSOCOM', $result);
		return $result;
	}
	
	/**
	 * Search player given a name.
	 * @return array of names and IDs
	 * @param object $name
	 */
	function search_player_by_name($name, $limit)
	{
		// Custom-wrote query required here
		// First query: data
		// Second query: total found rows
		$sql = 'SELECT SQL_CALC_FOUND_ROWS name, user_id, rating, tier 
				FROM players WHERE name LIKE ? AND ip != "0.0.0.0" ORDER BY rating DESC LIMIT ?';
		$query = $this->db->query($sql, array('%'.$name.'%', $limit));
		$result = $query->result_array();
		//---------------------------------------------
		$query = $this->db->query('SELECT FOUND_ROWS()');
		$temp = $query->row_array();
		$result['num_rows'] = $temp['FOUND_ROWS()'];
		return $result;
	}
	
	/**
	 * Get ID of a user, given username.
	 * @return 
	 * @param object $username
	 */
	function get_id_by_username($username)
	{
		$this->db->select('user_id');
		$this->db->from('players');
		$this->db->where('name', $username);
		
		// Run
		$query = $this->db->get();
		$result = $query->row_array();
		if ($query->num_rows() > 0)
		{
			return $result['user_id'];
		}
		else
		{
			return FALSE;
		}
	}
	
	/**
	 * Get Accuracy Stats of all weapons, given ID.
	 * @return 
	 * @param object $id
	 */
	function get_acc_by_id($id)
	{
		$query = $this->db->get_where('accuracy', array('user_id'=>$id));
		$result = $query->row_array();
		
		return $result;
	}
	
	/**
	 * Get user_ids, given an array of IP addresses.
	 * @return user_id
	 * @param object $ip array
	 */
	function get_ids_by_ip_array($ip)
	{
		$this->db->select('user_id, ip');
		$this->db->from('players');
		$this->db->where_in('ip', $ip);
		// Run
		$query = $this->db->get();
		$result = $query->result_array();
		
		return $result;
	}
	
	/**
	 * Get player stats summary
	 * @return stats summary
	 * @param object $id
	 */
	function get_player_quick_stats($id)
	{
		$this->db->select('tier, rating, plusminus');
		$this->db->from('players');
		$this->db->where('user_id', $id);
		// Run
		$query = $this->db->get();
		$result = $query->result();
		
		return $result;
	}
	
	/**
	 * Update IP if it's changed since last visit.
	 * @return TRUE if changed and updated, FALSE if not changed
	 * @param object $id
	 */
	function update_ip_if_changed($id)
	{
		// Get current IP
		$ip = $this->input->ip_address();
		// Get IP in database
		$this->db->select('ip');
		$this->db->from('players');
		$this->db->where('user_id', $id);
		$query = $this->db->get();
		// Have IP Changed? Yes:
		if ($ip != $query->row()->ip)
		{
			$query = $this->db->query("UPDATE players p, accuracy a SET p.ip='$ip',
						a.ip='$ip' WHERE a.user_id='$id' AND p.user_id='$id'");
			return TRUE;
		}
		else 
		{
			// IP remains unchanged
			return FALSE;
		}
	}
	
	/**
	 * Add players to the player table if the record does not exist.
	 * Used on the first visit after registering at Fracs BBS.
	 * @return 
	 * @param object $id
	 */
	function add_player_if_not_exist($id)
	{
		// Check for existence
		$query = $this->db->get_where('players', array('user_id'=>$id));
		if ($query->num_rows() == 0) // Does not exist; add to table.
		{
			// Add to 'players'
			$data = array
			(
				'user_id' => $id,
				'name' => $this->auth->dbase->convert2_username($id),
			);
			$this->db->insert('players', $data);
			
			// Add to 'accuracy'
			$data2['user_id'] = $id;
			$this->db->insert('accuracy', $data2);
		}
	}
}
/*
 * End of file
 */
